package com.gm.lib.utils;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;

import com.gm.lib.R;

/**
 * Created by cx on 15/4/21.
 */
public class GMProgressDialogUtil {

    private static ProgressDialog mProgressDlg;

    public static Dialog showProgressDialog(Context context) {
        return showProgressDialog(context, R.string.loading, true);
    }

    public static Dialog showProgressDialog(Context context, int res) {
        return showProgressDialog(context, res, true);
    }
    public static Dialog showProgressDialog(Context context, int res, boolean cancelable) {
        return showProgressDialog(context, context.getString(res), cancelable);
    }

    public static Dialog showProgressDialog(Context context, String message, boolean cancelable) {
        if (context == null || (context instanceof Activity && ((Activity) context).isFinishing())) {
            return null;
        }

        if (mProgressDlg != null) {
            mProgressDlg.cancel(); //防止执行两次showProgressDialog()
            mProgressDlg = null;
        }
        mProgressDlg = new ProgressDialog(context); //每次用时确保是新的

        mProgressDlg.setMessage(message);
        mProgressDlg.setCancelable(cancelable);
        mProgressDlg.show();
        return mProgressDlg;


    }

    public static void updateProgressDlg(String message) {
        if (mProgressDlg != null) {
            mProgressDlg.setMessage(message);
        }
    }


    public static void cancelProgressDialog() {
        if (mProgressDlg != null) {
            mProgressDlg.cancel();
            mProgressDlg = null;
        }
    }


/**
 * 得到自定义的progressDialog
 *
 * @param context
 * @param msg
 * @return
 */
    /*public static Dialog createLoadingDialog(Context context, String msg) {

		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.custom_progress_dialog, null);// 得到加载view
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
		// main.xml中的ImageView
		ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
		TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
		// 加载动画
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
				context, R.anim.loading_anim);
		// 使用ImageView显示动画
		spaceshipImage.startAnimation(hyperspaceJumpAnimation);
		tipTextView.setText(msg);// 设置加载信息

		Dialog loadingDialog = new Dialog(context, R.style.loding_dialog);// 创建自定义样式dialog

		//loadingDialog.setCancelable(false);// 不可以用“返回键”取消
		loadingDialog.setContentView(layout);// 设置布局
		return loadingDialog;

	}
	//private static ProgressDialog mProgressDlg;
	public static void showProgressDialog(Context context,int res,boolean cancelable){
		showProgressDialog(context, context.getString(res),cancelable);
	}
	public static void showProgressDialog(Context context,String message,boolean cancelable){
		 if(mProgressDlg !=null){
				mProgressDlg.cancel(); //防止执行两次showProgressDialog()
				mProgressDlg = null;
			}
			mProgressDlg = createLoadingDialog(context,message); //每次用时确保是新的
			mProgressDlg.setCancelable(cancelable);
			mProgressDlg.show();
		 ;
	}	*/
}
